package com.example.cc.server.controller;

import com.example.cc.common.constants.ConfigConstants;
import com.example.cc.common.dto.RegisterDTO;
import com.example.cc.server.service.ClientRegisterMemoryService;
import com.example.cc.server.service.ConfigurationMemoryService;
import com.example.cc.server.service.IConfigurationsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.Map;
import java.util.Optional;

/**
 * 1.使用内存模型简化操作，持久化放到下个版本
 * 2.使用 ResponseEntity 简化 HTTP响应
 */
@RestController
@RequestMapping(ConfigConstants.API_PATH)
@SuppressWarnings("all")
public class ConfigController {

    private final ConfigurationMemoryService configurationService;
    private final ClientRegisterMemoryService clientRegisterMemoryService;
    private final IConfigurationsService configurationsService;

    @Autowired
    public ConfigController(ConfigurationMemoryService configurationService, ClientRegisterMemoryService clientRegistrationService,
                            IConfigurationsService configurationsService) {
        this.configurationService = configurationService;
        this.clientRegisterMemoryService = clientRegistrationService;
        this.configurationsService = configurationsService;
    }

    /**
     * 注册客户端
     *
     * @param registerDTO
     * @return
     */
    @PostMapping("/register")
    public ResponseEntity<Void> registerClient(@RequestBody RegisterDTO registerDTO) {
        clientRegisterMemoryService.register(registerDTO);
        return ResponseEntity.ok().build();
    }

    /**
     * 注销客户端
     *
     * @param clientId
     * @return
     */
    @DeleteMapping("/register/{clientId}")
    public ResponseEntity<Void> unregisterClient(@PathVariable String clientId) {
        clientRegisterMemoryService.unregister(clientId);
        return ResponseEntity.noContent().build();
    }

    /**
     * 根据key获取配置信息
     *
     * @param key
     * @return
     */
    @GetMapping("/get/{key}")
    public ResponseEntity<Object> getConfigByKey(@PathVariable String key) {
        Optional<Object> optional = configurationService.getConfigByKey(key);
        return optional.map(ResponseEntity::ok).orElseGet(() -> ResponseEntity.notFound().build());
    }

    /**
     * 获取所有配置信息
     *
     * @return
     */
    @GetMapping("/getAll")
    public Map<String, Object> getAllConfigs() {
        return configurationService.getAllConfigs();
    }

    /**
     * 创建配置信息
     *
     * @param key
     * @param value
     * @return
     */
    @PostMapping("/create")
    public ResponseEntity<String> createConfig(@RequestParam String key, @RequestParam Object value) {
        if (configurationService.exists(key)) {
            return ResponseEntity.status(HttpStatus.CONFLICT).body("配置项已经存在，无法更新");
        }
        configurationService.create(key, value);
        return ResponseEntity.ok("Configuration created");
    }

    /**
     * 更新配置信息
     *
     * @param key
     * @param value
     * @return
     */
    @PutMapping("/{key}")
    public ResponseEntity<String> updateConfig(@PathVariable String key, @RequestParam String value) {
        configurationService.update(key, value);
        return ResponseEntity.ok("Configuration updated");
    }

    /**
     * 删除配置信息
     *
     * @param key
     * @return
     */
    @DeleteMapping("/{key}")
    public ResponseEntity<String> deleteConfig(@PathVariable String key) {
        if (!configurationService.delete(key)) {
            return ResponseEntity.notFound().build();
        }
        return ResponseEntity.ok("Configuration deleted");
    }
}
